define([
        'app/utils/UploadLocalVideoUtils',
        'app/utils/WebUtils',
        'app/service/GeneralServices',
        'underscore',
        'app/ui/StagingUI',
        'app/ui/RoomPwdUI',
        'app/ui/RoomLectureUI',
        'app/utils/UploadLogoUtils',
        'app/utils/UploadStagingUtils',
        'app/utils/UploadMutiplePicUtils',
        'app/utils/UploadCoverUtils',
        'app/utils/AMUICookieUtils',
        'app/utils/CommonUtils'
        
        
        ], function (
        UploadLocalVideoUtils, 
        WebUtils,
        gs,
        _,
        StagingUI,
        RoomPwdUI,
        RoomLectureUI,
        UploadLogoUtils,
        UploadStagingUtils,
        UploadMutiplePicUtils,
        UploadCoverUtils,
        AMUICookieUtils
        
    ) {
	var opt ={};
	var p1 = {};
	
	opt = p1 = AMUICookieUtils.initializePage();
	
	opt.copyrighttext = "© 2016 版权所有 中研沃兮网络科技（上海）有限公司";
	
	opt.roomuuid = p1.roomuuid = $.getUrlParam("roomuuid");
	
	WebUtils.openAmTab();
	
	gs.wjt_room_details_init(p1, function(resp){
		if(resp.status == "500"){
			if($.isNullStr(resp.result)){
				WebUtils.alert("连接错误，请刷新页面重试！");
			}else{
				WebUtils.alert(resp.result);
			}
			return;
		}
		
		opt.navi_main = "编辑／完善信息";
		opt.navi_sub = "完善你创建的微讲堂信息";
		
		WebUtils.inputInt($("#wjt-admin-copyright"), opt.copyrighttext);
		
		WebUtils.adminPageInt(opt);
		
		var room = resp.result.room;
		var ratio = parseFloat(resp.result.ratio);
		var lps = resp.result.lps;
		
		//Tab1
		WebUtils.inputInt($("#wjt_room_name"), room.name);
		WebUtils.inputInt($("#wjt_room_des"), room.des);
		WebUtils.inputInt($("#wjt_room_starttime"), $.timeFormat2(room.time));
		WebUtils.inputInt($("#wjt_room_endtime"), $.timeFormat2(room.endtime));
		WebUtils.inputInt($("#wjt_room_chair"), room.roomchairs);
		
		
		//select value_options
		var catalogs = resp.result.catalogs;
		if(typeof(catalogs.length) !="undefined"){
			
			if(catalogs.length > 2){

				$("#wjt_room_catalog").html("");
				$("#wjt_room_catalog").append("<option value=\"\">不选择</option>");
				
				for(var i = 2; i < catalogs.length; i++){
					var option_item = "<option value=\""+catalogs[i].uuid+"\">"+catalogs[i].name+"</option>";
					$("#wjt_room_catalog").append(option_item);	
				}
				
				$(".wjt_room_catalog_container").show();
			}else{
				$(".wjt_room_catalog_container").hide();
			}
		}
		var cataloguuid = resp.result.cataloguuid;
		WebUtils.inputInt($("#wjt_room_catalog"), cataloguuid);
		
		//Tab2
		WebUtils.imageInt($("#wjt_logo_upload_preview"), room.thumburl);
		WebUtils.imageInt($("#wjt_cover_upload_preview"), room.livecoverurl);
		
		//Tab3
		RoomLectureUI.initLecturePanel(ratio, lps, opt.useruuid, opt.roomuuid);
		
		//Tab4
		WebUtils.imageInt($("#wjt_staging_upload_preview"), room.coverurl);
		if($.isNullStr(room.doortemplate)){
			StagingUI.hideStagingPanel(opt.useruuid,opt.roomuuid);
		}else{
			StagingUI.showStagingPanel(opt.useruuid,opt.roomuuid);
		}
		
		//Tab5
		RoomPwdUI.initPwdPanel(room.pwd, opt.useruuid, opt.roomuuid);
	});
	
	//Tab1
	$(".wjt-room-basic-cancel").click(function(){
		WebUtils.inputRevert($("#wjt_room_name"));
		WebUtils.inputRevert($("#wjt_room_des"));
		WebUtils.inputRevert($("#wjt_room_starttime"));
		WebUtils.inputRevert($("#wjt_room_endtime"));
		WebUtils.inputRevert($("#wjt_room_catalog"));	
		WebUtils.inputRevert($("#wjt_room_chair"));
	});
	
	$(".wjt-room-basic-save").click(function(){

		var name = $.val_trim($("#wjt_room_name").val());
		var starttime = $.val_trim($("#wjt_room_starttime").val());
		var endtime =  $.val_trim($("#wjt_room_endtime").val());
		var des = $.val_trim($("#wjt_room_des").val());
		var cataloguuid = $.val_trim($("#wjt_room_catalog").val());
		var roomchairs = $.val_trim($("#wjt_room_chair").val());
		if($.isNullStr(name)){
			WebUtils.alert("微讲堂名称不能为空");
			return;
		}
		if(name.length >50){
			WebUtils.alert("微讲堂名称不能超过50字");
			return;
		}
		if($.isNullStr(starttime)){
			WebUtils.alert("请设置开始时间");
			return;
		}
		
		var $btn = $(this);
		$btn.button('loading');
		var p ={
			"useruuid":opt.useruuid,
			"roomuuid":opt.roomuuid,
			"name":name,
			"starttime":starttime,
			"endtime":endtime,
			"des":des,
			"cataloguuid":cataloguuid,
			"roomchairs":roomchairs
		};
		

		gs.wjt_room_basic_update(p,function(resp){
			
			$btn.button('reset');
			
			if(resp.status == "500"){
				if($.isNullStr(resp.result)){
					WebUtils.alert("操作不成功，请重试！");					
				}else{
					WebUtils.alert(resp.result);
				}
			}else{
				WebUtils.alert("修改成功");
				
				WebUtils.inputOriginalUpdate($("#wjt_room_name"));
				WebUtils.inputOriginalUpdate($("#wjt_room_des"));
				WebUtils.inputOriginalUpdate($("#wjt_room_starttime"));
				WebUtils.inputOriginalUpdate($("#wjt_room_endtime"));
				WebUtils.inputOriginalUpdate($("#wjt_room_catalog"));
				WebUtils.inputOriginalUpdate($("#wjt_room_chair"));
			}
			
		});
		
		
	});
	//Tab1 - end

	
	//tab2 - logo

	var url = "/wjt/upload_wjt_logo/"+opt.roomuuid;
	UploadLogoUtils.initSingleImage("wjt_logo_upload_container","wjt_logo_upload_btn",url,
		function(up, file, info){
			console.log(up);
			console.log(file);
			console.log(info);
			var resp = JSON.parse(info.response);
			console.log(resp);

			//tab2 refresh...
			$.setUrlParam("tab","2");
			//"{"status":"200","result":{"path":"http://localhost:8014/attachment/img/ac3f7f1d-4462-4362-8363-8a08826ebe82","key":"ac3f7f1d-4462-4362-8363-8a08826ebe82"}}"
		});
	
	var url = "/wjt/upload_wjt_cover/"+opt.roomuuid;
	UploadCoverUtils.initSingleImage("wjt_cover_upload_container","wjt_cover_upload_btn",url,
		function(up, file, info){
			console.log(up);
			console.log(file);
			console.log(info);
			var resp = JSON.parse(info.response);
			console.log(resp);

			//tab2 refresh...
			$.setUrlParam("tab","2");
			//"{"status":"200","result":{"path":"http://localhost:8014/attachment/img/ac3f7f1d-4462-4362-8363-8a08826ebe82","key":"ac3f7f1d-4462-4362-8363-8a08826ebe82"}}"
		});
	
	
	
	//tab2 - end
	
	
	//tab3 - start
	var url = "/wjt/upload_lecture_ppt_pic/"+opt.roomuuid;
	UploadMutiplePicUtils.initMutipleImage("wjt_add_pic_container","wjt_add_pic_btn",url,
		function(up, files){
			console.log(up);
			console.log(files);

			//tab3 refresh...
			$.setUrlParam("tab","3");
			//"{"status":"200","result":{"path":"http://localhost:8014/attachment/img/ac3f7f1d-4462-4362-8363-8a08826ebe82","key":"ac3f7f1d-4462-4362-8363-8a08826ebe82"}}"
		});
	//以下为本地视频上传
	var url = "/basic/qiniu/upload_vid/folder/"+opt.roomuuid;
	UploadLocalVideoUtils.initLocalVideo("wjt_add_loc_vid_container","wjt_add_loc_vid_btn",url,
		function(up, files){
			console.log(up);
			console.log(files);

			//tab3 refresh...
			$.setUrlParam("tab","3");
			//"{"status":"200","result":{"path":"http://localhost:8014/attachment/img/ac3f7f1d-4462-4362-8363-8a08826ebe82","key":"ac3f7f1d-4462-4362-8363-8a08826ebe82"}}"
		});
	//tab4 - start 
	var url = "/wjt/upload_wjt_staging/"+opt.roomuuid;
	UploadStagingUtils.initSingleImage("wjt_staging_upload_container","wjt_staging_upload_btn",url,
		function(up, file, info){
			console.log(up);
			console.log(file);
			console.log(info);
			var resp = JSON.parse(info.response);
			console.log(resp);

			//tab4 refresh...
			$.setUrlParam("tab","4");
			//"{"status":"200","result":{"path":"http://localhost:8014/attachment/img/ac3f7f1d-4462-4362-8363-8a08826ebe82","key":"ac3f7f1d-4462-4362-8363-8a08826ebe82"}}"
		});
	
	var $radios = $('[name="room-staging-ratio-options"]');
    $radios.on('change',function() {
    	if($radios.filter(':checked').val() == "1"){
    		StagingUI.showStagingPanel(opt.useruuid,opt.roomuuid,true);
    	}else{
    		StagingUI.hideStagingPanel(opt.useruuid,opt.roomuuid,true);
    	}
    });
	//tab4 - end

	
	return;
});